package com.usian.controller;

import com.usian.feign.ContentFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/backend/content")
@RestController
public class ContentController {

    @Autowired
    ContentFeign contentFeign;

    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(Long ids){
        Integer num = contentFeign.deleteContentByIds(ids);
        if (num == 1) {
            return Result.ok("OK");
        }
        return Result.error("删除失败");
    }


    /**
     * 内容管理  添加接口
     *
     * @param tbContent
     * @return
     */
    @RequestMapping("insertTbContent")
    public Result insertTbContent(TbContent tbContent){
        Integer num = contentFeign.insertTbContent(tbContent);
        if (num == 1) {
            return Result.ok("OK");
        }
        return Result.error("添加失败");
    }

    /**
     * 内容管理查询接口
     *
     * @param categoryId
     * @param pages
     * @param rows
     * @return
     */
    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(Long categoryId, @RequestParam(defaultValue = "1") Integer pages,
                                                 @RequestParam(defaultValue = "5") Integer rows){
       PageResult pageResult =  contentFeign.selectTbContentAllByCategoryId(categoryId,pages,rows);
        if (pageResult != null){
            return Result.ok(pageResult);
        }
        return Result.error("查询失败");
    }

    /**
     * 内容分类管理修改接口
     *
     * @param tbContentCategory
     * @return
     */
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(TbContentCategory tbContentCategory){
        Integer num = contentFeign.updateContentCategory(tbContentCategory);
        if (num == 1) {
            return Result.ok("OK");
        }
        return Result.error("修改失败");
    }


    /**
     * 内容分类管理删除接口
     *
     * @param categoryId
     * @return
     */
    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam("categoryId") Long categoryId) {
        Integer num = contentFeign.deleteContentCategoryById(categoryId);

        if (num == 1) {
            return Result.ok("OK");
        }
        return Result.error("删除失败");
    }

    /**
     * 内容分类管理添加接口
     *
     * @param tbContentCategory
     * @return
     */
    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(TbContentCategory tbContentCategory) {
        Integer num = contentFeign.insertContentCategory(tbContentCategory);
        if (num == 1) {
            return Result.ok("OK");
        }
        return Result.error("添加失败");
    }

    /**
     * 内容分类管理查询接口
     *
     * @param id
     * @return
     */
    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Integer id) {
        List<TbContentCategory> list = contentFeign.selectContentCategoryByParentId(id);
        if (list != null && list.size() > 0) {
            return Result.ok(list);
        }
        return Result.error("查询失败");
    }

}
